using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

namespace CodeDom
{
  public class Field
  {
    private readonly SitecoreConnection _connection;
    private readonly Guid _templateFieldID;
    private TemplateField _templateField;
    private readonly string _value;

    internal Field(XmlElement element, SitecoreConnection connection)
    {
      _connection = connection;
      _templateFieldID = new Guid(element.Attributes["tfid"].Value);
      _value = element.SelectSingleNode("content").InnerXml;
    }

    private SitecoreConnection Connection
    {
      get { return _connection; }
    }

    public string Value {
      get {
        return _value;
      }
    }

    public TemplateField TemplateField
    {
      get
      {
        if (_templateField == null)
          _templateField = Connection.GetTemplateField(TemplateFieldID);
        return _templateField;
      }
    }

    public Guid TemplateFieldID
    {
      get
      {
        return _templateFieldID;
      }
    }
    
    public string Name
    {
      get {
        return TemplateField == null ? null : TemplateField.Name;
      }
    }
  }
}